/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2025-01-07
 * Time: 12:43
 */
class Solution5 {
    public String multiply(String num1, String num2) {
        if(num1.charAt(0) == '0' || num2.charAt(0) == '0') return "0";
        int n1 = num1.length() , n2 = num2.length();
        int n3 = n2 + n1 - 1;
        int[] nums3 = new int[n3];

        StringBuilder builder1 = new StringBuilder(num1);
        StringBuilder builder2 = new StringBuilder(num2);
        String str1 = builder1.reverse().toString();
        String str2 = builder2.reverse().toString();



        for(int i = 0 ; i < n1 ; i++){
            for(int j = 0 ; j < n2 ; j++){
                nums3[i+j] += (str1.charAt(i)-'0')*(str2.charAt(j)-'0');
            }
        }
        StringBuilder builder3 = new StringBuilder();
        int tmp = 0;
        for(int j = 0 ; j < n3 ; j++){
            tmp = nums3[j]/10;//进位的
            builder3.append((char)(nums3[j]%10 + '0'));
            if(j < n3-1){
                nums3[j+1] += tmp;
            }
            if(j == n3-1 && nums3[j] >= 10){
                builder3.append((char)(tmp + '0'));
            }
        }
        String ret = builder3.reverse().toString();
        return ret;
    }
}
public class Test5 {
    public static void main(String[] args) {
        Solution5 solution5 = new Solution5();
        String num1 = "140";
        String num2 = "721";
        solution5.multiply(num1,num2);
    }
}
